const enterpriseVue = new Vue({
    el: '#app',
    data() {
        return {
            rules: {
                Article_Name: [{ required: true, message: '请输入商品名称', trigger: 'blur' }],
                Brand_SerialNumber: [{ required: true, message: '请选择商品品牌', trigger: 'change' }],
                Category_SerialNumber: [{ required: true, message: '请选择商品类别', trigger: 'change' }],
                Series_SerialNumber: [{ required: true, message: '请选择商品系列', trigger: 'change' }],
                Item_SerialNumber: [{ required: true, message: '请选择商品款型', trigger: 'change' }],
                Weight_Unit: [{ required: true, message: '请选择SKU单位', trigger: 'change' }],
                Unit_Price: [{ required: true, message: '请输入吊牌价', trigger: 'blur' }],
                Picture_URL: [{ required: true, message: '请输入图片地址', trigger: 'blur' }],
                App_ID: [{ required: true, message: '请输入App_ID', trigger: 'blur' }],
            },
            Specifications_Name: [{ required: true, message: '请输入规格', trigger: 'blur' }],
            Packing: [{ required: true, message: '请输入SKU数量', trigger: 'blur' }],
            PackingSize: [{ required: true, message: '请输入装箱尺寸', trigger: 'blur' }],
            OutsidePackingSize: [{ required: true, message: '请输入外箱尺寸', trigger: 'blur' }],
            Unit_Price: [{ required: true, message: '请输入零售价', trigger: 'blur' }],
            Net_Content: [{ required: true, message: '请输入净含量', trigger: 'blur' }],
            cagie: '',
            form1: {//储值卡方案表
                Prepayment_Card_Scheme_SerialNumber: "",//储值卡方案流水号
                Prepayment_Card_Scheme_Name: '',//储值卡方案名称
                Starting_Amount: '',//储值卡价格
                Gift_Amount: 0,//赠送值
                Discount: '',//享受折扣
                Execute_Date: '',//执行日期
                Permanent: '0',//是否长期有效 (0是,1不是)
                Effective_Date: '',//有效期(长期有效为1可选)
                Prepayment_Card_Page_SerialNumber: '',//储值卡版面
                Scheme_Type: 0,//储值卡类型
                Start_Date: '',//上架时间
                End_Date: '',//结束时间
                Payment_Type: '',//付款方式(1微信支付,2支付宝支付,3电汇)
                Amount: "",//总金额
                Apply_Article:'0',//0适用全部商品  1适用部分商品
                Collection_Amount: 0,//实付金额
                Collection_Date: "",//付款时间
                Buyer_Enterprise_Info_SerialNumber: '',//被制卡企业
                Effective: 0,//是否生效
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),//本企
                Children: {
                    prepayment_card_scheme_storescope: [{//适用店铺表
                        Prepayment_Card_Scheme_StoreScope_SerialNumber: "",//主键
                        Prepayment_Card_Scheme_SerialNumber: "",//外键
                        Store_SerialNumber: "",//门店流水号表
                    }],
                    prepayment_card_scheme_article: [{//适用商品表
                        Prepayment_Card_Scheme_Article_SerialNumber: "",//主键
                        Prepayment_Card_Scheme_SerialNumber: "",//外键
                        Article_SerialNumber: "",//商品主键
                    }],
                    prepayment_card_scheme_prompt: [
                        //     {//温馨提示表
                        //     Prepayment_Card_Scheme_Prompt_SerialNumber: "",//主键
                        //     Prepayment_Card_Scheme_SerialNumber: "",//外键
                        //     Prepayment_Card_Scheme_Prompt: "",//储值卡提示
                        //     Prepayment_Card_Scheme_Main: "",//储值卡提示内容
                        // }
                    ]
                }
            },
            form2: [{//储值卡编码表
                Prepayment_Card_SerialNumber: "",//编码表主键
                Prepayment_Card_Scheme_SerialNumber: "",//方案表主键
                Mobile: "",//会员电话
                Open_ID: "",//用户标识
                Start_DateStart_Date: '',//上架时间
                End_Date: '',//结束时间
                Discount: '',//折扣
                Prepayment: "",//预付款
                Prepayment_Sales: 0,//预付款消费
                Prepayment_Balance: "",//预付款余额
                Gift_Amount: "",//赠送金额
                Gift_Sales: 0,//赠送金额消费
                Gift_Balance: "",//赠送余额
                Apply_Article:0,//0适用全部商品  1适用部分商品
            }],
            fileXlsx: [],
            Articles: [],
            options: [{
                value: '选项1',
                label: '黄金糕'
            }, {
                value: '选项2',
                label: '双皮奶'
            }, {
                value: '选项3',
                label: '蚵仔煎'
            }, {
                value: '选项4',
                label: '龙须面'
            }, {
                value: '选项5',
                label: '北京烤鸭'
            }],
            radio: '2',
            II_total: 1,
            activeName: 'first',
            tableData: [],
            tableData1: [],
            loading: false,
            Employees: [],
            PayWay: [{
                value: '1',
                label: '微信支付'
            }, {
                value: '2',
                label: '支付宝支付'
            }, {
                value: '3',
                label: '电子汇账'
            },],
            Categorys: [{
                label: '按商品类别',
                value: '0',
            }, {
                label: '按商品品牌',
                value: '1',
            }],
            Enterprises: [],
            ArticleByCategorys: [],
            Stores: [],
            IO_form: {
                page: 1,
                limit: 10,
                Category_SerialNumber: JSON.stringify([]),
                Brand_SerialNumber: JSON.stringify([]),
                Article_Name: ""
            },
            IO_form1: {
                page: 1,
                limit: 10,
                Department_SerialNumber: JSON.stringify([]),
            },
            total1: 0,
            total2: 0,
            isPermanent: true,
            organization: [],
            props: {
                expandTrigger: 'hover',
                label: 'text',
                value: 'value',
                children: 'children',
                multiple: true
            },
            Cardpages:[],
            CardpagesImg:'',
            srcList:[]
        }
    },
    mounted() {
        this.getBuyer_Enterprise()
        this.getStores()
        this.getCardpage()
    },
    methods: {
        handleClick(tab, event) {
            console.log(tab.index);
            this.form1.Scheme_Type = tab.index
            console.log(this.form1);
            if (tab.index == 0) {
                this.form1.Effective = tab.index
            } else {
                this.form1.Effective = tab.index
            }
            this.form1 = {//储值卡方案表
                Prepayment_Card_Scheme_SerialNumber: "",//储值卡方案流水号
                Prepayment_Card_Scheme_Name: '',//储值卡方案名称
                Starting_Amount: '',//储值卡价格
                Gift_Amount: 0,//赠送值
                Discount: '',//享受折扣
                Execute_Date: '',//执行日期
                Permanent: '0',//是否长期有效 (0是,1不是)
                Effective_Date: '',//有效期(长期有效为1可选)
                Prepayment_Card_Page_SerialNumber: '',//储值卡版面
                Scheme_Type: tab.index,//储值卡类型
                Start_Date: '',//上架时间
                End_Date: '',//结束时间
                Apply_Article:'0',
                Payment_Type: '',//付款方式(1微信支付,2支付宝支付,3电汇)
                Amount: "",//总金额
                Collection_Amount: 0,//实付金额
                Collection_Date: "",//付款时间
                Buyer_Enterprise_Info_SerialNumber: '',//被制卡企业
                Effective: 0,//是否生效
                Enterprise_Info_SerialNumber: $.cookie("enterprise"),//本企
                Children: {
                    prepayment_card_scheme_storescope: [{//适用店铺表
                        Prepayment_Card_Scheme_StoreScope_SerialNumber: "",//主键
                        Prepayment_Card_Scheme_SerialNumber: "",//外键
                        Store_SerialNumber: "",//门店流水号表
                    }],
                    prepayment_card_scheme_article: [{//适用商品表
                        Prepayment_Card_Scheme_Article_SerialNumber: "",//主键
                        Prepayment_Card_Scheme_SerialNumber: "",//外键
                        Article_SerialNumber: "",//商品主键
                    }],
                    prepayment_card_scheme_prompt: [
                        //{//温馨提示表
                        // Prepayment_Card_Scheme_Prompt_SerialNumber: "",//主键
                        // Prepayment_Card_Scheme_SerialNumber: "",//外键
                        // Prepayment_Card_Scheme_Prompt: "",//储值卡提示
                        // Prepayment_Card_Scheme_Main: "",//储值卡提示内容
                        //}
                    ]
                }
            }
        },
        handleSizeChange1(val) {
            this.IO_form.page = 1;
            this.IO_form.limit = val;
        },
        //号码按键查询
        handleCurrentChange1(val) {
            this.IO_form.page = val;
        },
        handleSizeChange2(val) {
            this.IO_form1.page = 1;
            this.IO_form1.limit = val;
        },
        //号码按键查询
        handleCurrentChange2(val) {
            this.IO_form1.page = val;
        },
        uploadXLSX(file) {
            console.log(file)
            this.fileXlsx.push({
                name: file.name,
                url: file.type
            })
            const _this = this
            let arr = []
            var reader = new FileReader();
            reader.readAsBinaryString(file);
            reader.onload = function (e) {
                var data = e.target.result;
                var wb = XLSX.read(data, {
                    type: 'binary' //以二进制的方式读取
                });
                var sheet = wb.Sheets[wb.SheetNames[0]];//sheet0代表excel表格中的第一页
                var json = XLSX.utils.sheet_to_json(sheet);//利用接口实现转换。
                console.log(json);
                _this.tableData1 = json
                _this.tableData1.forEach((item, i) => {
                    item.Department_Name = json[i]['所属部门']
                    item.Organization_Name = json[i]['所属组织']
                    item.Employee_Phone = json[i]['员工手机号']
                })
                _this.total2 = _this.tableData1.length
            }
        },
        XlsxbeforeRemove() {
            this.fileXlsx.pop()
            this.tableData1 = []
        },
        getAddAction() {
            const _this = this;
            $.ajax({
                type: 'POST',
                url: DomainName + '/getBatchAddAction?token=' + $.cookie('token'),
                data: {
                    tableName: 'prepayment_card',
                    Data: JSON.stringify(_this.form2)
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                },
                success: function (Result) {
                    if (Result.Status == 'success') {
                        _this.$message.success('提交成功')
                        _this.loading = false
                        window.history.go(-1)
                    }
                },
                error: function () {
                    _this.$message.error("操作失败，请稍后再试")
                },
                dataType: "json"
            });
        },
        getBuyer_Enterprise() {
            const this_ = this
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data: 'enterprise_info',
                    isNullEnterprise: true
                },
                success: function (res) {
                    this_.Enterprises = res
                    this_.Enterprises = this_.Enterprises.filter(item => item.Enterprise_Name != '系统管理')
                    console.log(this_.Enterprises)
                }
            })
        },
        getEmployees(Enterprise_Info_SerialNumber) {
            const this_ = this
            $.ajax({
                url: DomainName + '/getOrganizationAction?token=' + $.cookie('token'),
                type: 'POST',
                data: {
                    Enterprise_Info_SerialNumber,
                },
                success: function (res) {
                    console.log(res)
                    this_.Employees = res.Data
                }
            })
        },
        EnterChange(e) {
            this.getEmployees(e)
        },
        getArticleInfo(data) {
            const this_ = this
            $.ajax({
                url: DomainName + '/getArticleInfoAction?token=' + $.cookie("token"),
                type: 'POST',
                data,
                success: function (res) {
                    console.log('商品',res)
                    this_.tableData = res.tableData
                    this_.total1 = Number(res.limit)
                }
            })
        },
        CategorysChange(e) {
            console.log(e)
            if (e == 0) {
                this.getCategorys('category')
            } else {
                this.getCategorys('brand')
            }
        },
        ArticlesChange(e) {
            if (e.length > 0) {
                if (this.cagie=='1') {
                    this.IO_form.Brand_SerialNumber = JSON.stringify(e)
                    console.log(this.IO_form)
                } else {
                    this.IO_form.Category_SerialNumber = JSON.stringify(e)
                    console.log(this.IO_form)
                }
                this.getArticleInfo(this.IO_form)
            }
        },
        EmployeesChange(e) {
            console.log(e)
            let arr = []
            e.forEach(item => {
                arr.push(item[1])
            })
            this.IO_form1.Department_SerialNumber = JSON.stringify(arr)
            this.getAllEmployees(this.IO_form1)
        },
        getAllEmployees(data) {
            const this_ = this
            $.ajax({
                url: DomainName + '/getEmployeeIDAction?token=' + $.cookie("token"),
                type: 'POST',
                data,
                success: function (res) {
                    console.log(res)
                    this_.tableData1 = res.Data
                    this_.total2 = res.Data.length
                }
            })
        },
        getCategorys(Data) {
            const this_ = this
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                type: 'POST',
                data: {
                    Data,
                    isNullEnterprise: true
                },
                success: function (res) {
                    console.log(res)
                    this_.ArticleByCategorys = res
                }
            })
        },
        getStores() {//获取所有店铺
            const this_ = this
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                data: {
                    Data: "store_info",
                },
                success: function (res) {
                    console.log(res);
                    this_.Stores = res
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        getCardpage() {//获取所有版面
            const this_ = this
            $.ajax({
                url: DomainName + '/getInquiryAction?token=' + $.cookie("token"),
                data: {
                    Data: "prepayment_card_page",
                },
                success: function (res) {
                    console.log(res);
                    this_.Cardpages = res
                },
                complete: function (XMLHttpRequest) {
                    if (XMLHttpRequest.getResponseHeader("REDIRECT") == "REDIRECT") {
                        window.parent.document.location.href = "../../../../" + XMLHttpRequest.getResponseHeader("CONTENTPATH");
                    }
                }
            })
        },
        SelectChange(e){
            // this.Cardpages
            this.Cardpages.forEach((item,i)=>{
                if(item.Prepayment_Card_Page_SerialNumber ==e){
                    this.CardpagesImg = item.Picture_URL
                }
            })
            this.srcList = []
            this.srcList.push(DomainIMG + this.CardpagesImg)
        },
        Delete(i) {//删除商品
            console.log(i)
            if (this.tableData.length > 1) {
                this.tableData.splice(i, 1)
                this.total1--
            } else {
                this.$message({
                    message: '不可以再删除了',
                    type: 'error'
                });
            }
        },
        AddTips() {//增加提升
            this.form1.Children.prepayment_card_scheme_prompt.push({//温馨提示表
                Prepayment_Card_Scheme_Prompt_SerialNumber: "",//主键
                Prepayment_Card_Scheme_SerialNumber: "",//外键
                Prepayment_Card_Scheme_Prompt: "",//储值卡提示
                Prepayment_Card_Scheme_Main: "",//储值卡提示内容
            })
        },
        RdcTips() {//减少提示
            // if (this.form1.Children.prepayment_card_scheme_prompt.length > 1) {
            this.form1.Children.prepayment_card_scheme_prompt.pop()
            // } else {
            //     this.$message({
            //         message: '不可以再删除了',
            //         type: 'error'
            //     });
            // }
        },
        AddStore() {//增加店铺
            this.form1.Children.prepayment_card_scheme_storescope.push({//适用店铺表
                Prepayment_Card_Scheme_StoreScope_SerialNumber: "",//主键
                Prepayment_Card_Scheme_SerialNumber: "",//外键
                Store_SerialNumber: "",//门店流水号表
            })
        },
        RdcStore() {//减少店铺
            if (this.form1.Children.prepayment_card_scheme_storescope.length > 1) {
                this.form1.Children.prepayment_card_scheme_storescope.pop()
            } else {
                this.$message({
                    message: '不可以再删除了',
                    type: 'error'
                });
            }
        },
        submit(formName) {//提交表单
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.form1.Discount == '') {
                        this.form1.Discount = 100
                    }
                    let arr = []
                    this.tableData.forEach(item => {
                        arr.push(item.Article_SerialNumber)
                    })
                    this.form1.Children.prepayment_card_scheme_article = this.tableData
                    this.form1.Children.prepayment_card_scheme_article.forEach((item, i) => {
                        item.Prepayment_Card_Scheme_Article_SerialNumber = ""//主键
                        item.Prepayment_Card_Scheme_SerialNumber = ""//外键
                        item.Article_SerialNumber = arr[i]
                    })
                    this.loading = true
                    const this_ = this
                    this.form1.Amount = this.form1.Starting_Amount * this.tableData1.length
                    console.log(this.form1);
                    $.ajax({
                        url: DomainName + '/getAddAction?token=' + $.cookie("token"),
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ prepayment_card_scheme: [this_.form1] }),
                        },
                        success: function (res) {
                            console.log(res)
                            if (res.Status == 'success' && this_.form1.Scheme_Type == 0) {
                                console.log(this_.form2)
                                this_.form2 = this_.tableData1
                                this_.form2.forEach((item, i) => {
                                    item.Prepayment_Card_Scheme_SerialNumber = res.Data
                                    item.Mobile = this_.tableData1[i].Employee_Phone
                                    item.Start_Date = this_.form1.Execute_Date
                                    item.End_Date = (this_.form1.Permanent == '1' ? this_.getTime(this.form1.Execute_Date, this.form1.Effective_Date) : '')
                                    item.Discount = this_.form1.Discount
                                    item.Prepayment = this_.form1.Starting_Amount
                                    item.Prepayment_Balance = this_.form1.Starting_Amount
                                    item.Gift_Amount = this_.form1.Gift_Amount
                                    item.Gift_Balance = this_.form1.Gift_Amount
                                    item.Open_ID = '-'
                                })
                                this_.getAddAction()
                            } else if (res.Status == 'success' && this_.form1.Scheme_Type == 1) {
                                this_.$message.success('提交成功')
                                this_.loading = false
                            }
                            window.history.go(-1)
                        }
                    })
                } else {
                    this.$message({
                        message: '请检查表单',
                        type: 'warning'
                    });
                }
            });
        },
        getTime(date, num) {
            let data = new Date(date).getTime() + (num * 24 * 60 * 60 * 1000)
            return getYMDHMSDate(data)
        },
        DeleteEmployee(i) {
            if (this.tableData1.length > 1) {
                this.tableData1.splice(i, 1)
                this.total2--
            } else {
                this.$message({
                    message: '不可以再删除了',
                    type: 'error'
                });
            }
        }
    }
})